
import mx.utils.Delegate;
import com.live5.liveplayer.MediaPlayer;
import com.live5.liveplayer.Playlist;
import com.live5.liveplayer.Mp3Controller;
import com.live5.controls.LiveProgressBar;
class com.live5.liveplayer.Mp3Player extends MediaPlayer
{
	var loadProgressInterval:Number;
	var playedSec:Number;
	var mp3Sound:Sound;
	var MediaController:Mp3Controller;

	function Mp3Player()
	{
		
		//mp3Controller=Mp3Controller(super.MediaController);
		MediaType="audio/mpeg";
		mp3Sound=new Sound();
		mp3Sound.onSoundComplete=Delegate.create(this,OnComplete);
		mp3Sound.onLoad=Delegate.create(this.OnLoad);
	}
	function DoPlay(){
		super.DoPlay();
		mp3Sound.loadSound(this.playlist.Current.Location,true);
	   MediaController.Mp3PBar.Reset();
		loadProgressInterval=setInterval(this,"UpdateLoadProgress",10);
	}
	function Stop(){
		
		mp3Sound.stop();
	}
	function OnLoad(success:Boolean){
		if(success){
			
		}
	}
	private function UpdateLoadProgress(){
		trace("updata progress");
		trace(mp3Sound.getBytesLoaded()+""+mp3Sound.getBytesTotal());
		MediaController.Mp3PBar.SetProgress(mp3Sound.getBytesLoaded(),mp3Sound.getBytesTotal());
		if(mp3Sound.getBytesLoaded()>=mp3Sound.getBytesTotal()){
			clearInterval(loadProgressInterval);
		}
	}
	function Pause(){
		
		if(this.isPaused){
			trace("mp3 resume");
			this.mp3Sound.start(playedSec);
		}else{
			playedSec = this.mp3Sound.getPosition()/1000;
			trace(playedSec);
			// Try to solve unloaded sound trick.
			mp3Sound.start(playedSec);
		    mp3Sound.stop();
		}
		super.Pause();
	}
}
